// pages/community/searchList.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      navbar: ['书评', '话题', '问答', '写文'],
      currentTab: 0,
    list:[],
      pagenum:1,
      cutype:0,
      cufrom:0,
      keyword:'',
      id:0,
      from:4,
      datainfo:{},
      pingLunData:{isShowPingLun:0,value:''},
      isShowPingLun:1,
      pingLunContent:'',
      bookImage:['1','1','1'],
      pinglunlists:[],
      liulannum:0
  },

    navbarTap: function(e){
        this.setData({
            currentTab: e.currentTarget.dataset.idx ,
            pageNum:1,
            cutype:parseInt(e.currentTarget.dataset.idx) + 1,
            cufrom:parseInt(e.currentTarget.dataset.idx) + 2
        })
        this.getDataList();
    },
    getDataList:function(e){
    var that = this;
        wx.request({
            url: app.data.server+'/index.php/Tongcheng/Book/getSheQuListBySearch?keyword='+that.data.keyword+'&pagenum='+that.data.pagenum+"&type="+that.data.cutype+"&uid="+app.globalData.userInfo.uid,
            success:function(d){
                // console.log('123'+JSON.stringify(d.data));
                that.setData({
                    list:d.data
                })
            }
        })
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var cutype = parseInt(that.data.currentTab)+1;
    var cufrom = parseInt(that.data.currentTab)+2;
    console.log(options)
    this.setData({
        cutype:cutype,
        cufrom:cufrom,
        keyword:options.keyword
    })
    this.getDataList();
  },

    hidePingLun:function(){
        this.setData({
            pingLunData:{isShowPingLun:0}
        })
    },


    showPingLun:function(e){
        var that = this;
        this.setData({
            pingLunData:{isShowPingLun:1},
            pingLunContent:'',
            id:e.currentTarget.dataset.id,
            from:e.currentTarget.dataset.from
        })
    },

    pingluninput:function(e){
        var that = this;
        this.setData({
            pingLunContent:e.detail.value
        })
    },
    fasong:function(e){
        getApp().globalData.iswangchengpinglun=0;
        var that = this;
        var formdata = {
            pingLunContent:this.data.pingLunContent,
            uid:getApp().globalData.userInfo.uid,
            otherid:that.data.id,
            from:that.data.from
        }

        var i = setInterval(function(e){
            // console.log(getApp().globalData.iswangchengpinglun);
            if(1 == getApp().globalData.iswangchengpinglun){
                that.hidePingLun();
                that.setData({
                    list:[
                    ]
                })
                that.getDataList();
                clearInterval(i);
            }
        },1000)
        // console.log(JSON.stringify(formdata))
        app.pinglun(formdata);

    },//点赞
    dianzan:function(e){
        var that = this;
        var from = e.currentTarget.dataset.from;
        var id = e.currentTarget.dataset.id;
        var formdata = {
            otherid:id,
            from:from
        }
        app.dianZan(formdata);
        //修改当前数据源，避免重新请求数据
        //3社区的书评 4社区的话题 5社区的问答 6社区的写文
        var temp = that.data.list;
        // console.log(temp);
        for(var i=0;i<temp.length;i++){
            var item = temp[i];
            if(id == item.id){
                if(1 == item.isDianZan){
                    item.dianzan_num=--item.dianzan_num;
                    item.isDianZan=0;
                }else if(0 == item.isDianZan){
                    item.dianzan_num=++item.dianzan_num;
                    item.isDianZan=1;
                }
            }
        }
        that.setData({
            list:temp
        })
    },
    onShareAppMessage: function (res) {
        // if (res.from === 'button') {
        //   // 来自页面内转发按钮
        //   console.log(res.target)
        // }
        var id = res.target.dataset.id;
        var path = '/pages/community/xiewen/info?id='+id;
        return {
            title: '分享',
            path: path
        }
    },
    toShuPingInfo:function(e){
        var id = e.currentTarget.dataset.id;
        console.log('hot'+id)
        wx.navigateTo({
            url:'/pages/community/shuping/info?id='+id
        })
    }
    ,
    toHuaTiInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/community/huati/info?id='+id
        })
    }
    ,
    toWenDaInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/community/wenda/info?id='+id
        })
    }
    ,
    toXieWenInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/community/xiewen/info?id='+id
        })
    },
})